import { reqGoodsInfo, reqAddOrUpdateShopCart } from '@/api'
// 封装游客省份模块uuid
import { getUUID } from '@/urils/uuid_token'

const state = {
  goodInfo: {},
  uuid_token: getUUID()
}
const mutations = {
  GETGOOINFO(state, goodInfo) {
    state.goodInfo = goodInfo
  }
}

const actions = {
  // 获取产品信息的action
  async getGooInfo({ commit }, skuId) {
    let result = await reqGoodsInfo(skuId)
    if (result.code == 200) {
      commit('GETGOOINFO', result.data)
    }
  },
  // 将产品添加到购物车中
  async addOrUpdateShopCart({ commit }, { skuId, skuNum }) {
    //  加入购物车返回的解构
    let result = await reqAddOrUpdateShopCart(skuId, skuNum)
    //  表示请求成功
    if (result.code == 200) {
      return 'ok'
    } else {
      // 请求失败 
      return Promise.reject(new Error('faile'))
      // return 'false'
    }
  }
}

const getters = {
  // 路径导航简化的数据
  categoryView(state) {
    return state.goodInfo.categoryView || {};
  },
  // 简化产品信息的数据
  skuInfo(state) {
    return state.goodInfo.skuInfo || {}
  },
  // 产品售卖属性的简化
  spuSaleAttrList(state) {
    return state.goodInfo.spuSaleAttrList || []
  }
}

export default {
  state, mutations, actions, getters
}